Summit.UI.Widget.Hour_Viewer = function(settings)
{
	var self = this;
	
	this.settings = {
		format: 12, /* 12=12 Hour, 24=24 Hour */
		width:300,
		dayClickHandler:function(Date){},
		agenda:[],
		agendaSyntax:"%count% Items",
		time:0,
		monthNames : new Array("January","February","March","April","May","June","July","August","September","October","November","December")
	}
	
	
	this.selectedRow = false;
	this.painted = false;
	this.el={};
	
	for(i in settings)
	this.settings[i] = settings[i];
	
	/**
	 * Quick Function That Returns Date.getHours In 12 Hour Format
	 */
	this.get12HourName = function(hour)
	{
		if(hour==0)
		return "12 AM";
		else if (hour==12)
		return "12 PM";
		else if(hour<12)
		return hour + " AM";
		else
		return hour-12 + " PM"
	}
	
	this.getAgenda = function(hour)
	{
		var count = 0;
	
			
		if(self.settings.agenda)
		{
			if(self.settings.agenda[hour])
			count = self.settings.agenda[hour];
		}
		
		return self.settings.agendaSyntax.replace('%count%',count);
	}
	
	this.selectRow = function(hourRow)
	{
		if(parent.selectedRow)
		{
			$each(parent.selectedRow.childNodes,function(child){
				child.style.borderColor = '';
			})
		}
		
		parent.selectedRow = hourRow.el.tr;
		
		$each(parent.selectedRow.childNodes,function(child){
			child.style.borderColor = '#CCCCCC';
		})
	}
	
	this.hourRow = function(hour,parent)
	{
		var self = this;
		
		this.el ={};
		this.el.tr = new Element('tr',{
			'styles':{
				'color':'#F8CC00',
				'cursor':'pointer'
			},
			'events':{
				'click':function(event)
				{
					parent.settings.time.setHours(hour)
					parent.settings.time.setMinutes(0);
					parent.settings.time.setSeconds(0);

					parent.selectRow(self);				
								
					parent.settings.dayClickHandler(parent.settings.time);
				}
			}
		});
		
		this.el.titleTd = new Element('td',{
			'class':'row1',
			'align':'center',
		}).setText(parent.get12HourName(hour)).injectInside(this.el.tr);
		
		this.el.agendaTd = new Element('td',{
			'class':'row3',
			'width':"80%",
			'align':'center'
		}).setText(parent.getAgenda(hour)).injectInside(this.el.tr);
		
		this.moveTo = function(parent)
		{
			self.el.tr.injectInside(parent);
		}
		
		if(hour == parent.settings.time.getHours())
		{
			parent.selectRow(self);
		}
	}	

	this.paint = function()
	{
		if(self.settings.time == 0)
		self.settings.time = new Date();
				
		if(!self.painted)
		{		
			self.el.mainDiv = new Element("div",{
				'styles':{
					'width':self.settings.width,
					'text-align':'center'
				}
			});
		}
		else
		{
			self.el.mainDiv.empty();
		}
		
		self.el.mainTbl = new Element('table',{
			'class':'forumline',
			'styles':{
				'width':'100%',
				'margin-bottom':'10px'
			}
		}).injectInside(self.el.mainDiv);
		
		self.el.dayTitleTr = new Element('tr').injectInside(self.el.mainTbl);
		self.el.dayTitleTh = new Element('th',{'colspan':2}).injectInside(self.el.dayTitleTr);
		self.el.dayTitleTh.setText(self.settings.monthNames[self.settings.time.getMonth()] + " " + self.settings.time.getDate() + ", " + self.settings.time.getFullYear() + " - " + (self.settings.time.getHours() < 12 ? 'AM' : 'PM'));
		
		var start = (Math.floor(self.settings.time.getHours()/12)*12);
		var end = start+12;
		
		for(var i=start; i<end; i++)
		{
			var row = new self.hourRow(i,self);
			row.moveTo(self.el.mainTbl);
		}
		
		self.el.amButton = new Element("a",{
			'href':'#',
			'events':{
				'click':function(event)
				{
					if(self.settings.time.getHours()>=12)
					{
						self.settings.time.setHours(self.settings.time.getHours()-12);
						self.paint();
						self.settings.dayClickHandler(self.settings.time);
					}
				}
			}
		}).setText("AM").injectInside(self.el.mainDiv);
		
		self.el.mSpacer = new Element("b").setHTML("&nbsp;&nbsp;&bull;&nbsp;&nbsp;").injectInside(self.el.mainDiv);
		
		self.el.pmButton = new Element("a",{
			'href':'#',
			'events':{
				'click':function(event)
				{
					if(self.settings.time.getHours()<12)
					{
						self.settings.time.setHours(self.settings.time.getHours()+12);
						self.paint();
						self.settings.dayClickHandler(self.settings.time);
					}
					
				}
			}
		}).setText("PM").injectInside(self.el.mainDiv);
		
		self.painted=true;
	}
	
	/**
	 *	Move Viewer
	 */
	this.moveTo=function(parent)
	{
		if(!self.painted)
		self.paint();
				
		self.el.mainDiv.injectInside(parent);
	}
	
	/**
	 * Shows Schedule
	 */
	this.show=function()
	{
		self.el.mainDiv.style.display='';
	}

	/**
	 * Hides Schedule
	 */
	this.hide=function()
	{
		self.el.mainDiv.style.display='none';
	}
}